package com.ray.auth.system.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.ray.auth.system.emun.LinkType;
import com.ray.auth.system.service.ButtonService;
import com.ray.auth.system.service.DictService;
import com.ray.auth.system.service.OrganizationService;
import com.ray.auth.system.service.RoleService;
import com.ray.auth.system.service.UserService;
import com.ray.auth.system.table.entity.Button;
import com.ray.auth.system.table.entity.Dict;
import com.ray.auth.system.table.entity.Organization;
import com.ray.auth.system.table.entity.Role;
import com.ray.auth.system.table.entity.User;
import com.ray.common.util.DataGridUtil;
import com.ray.common.util.DictLoadUtil;
import com.zjhc.flyhong.common.function.operator.LoginUser;
import com.zjhc.flyhong.common.function.operator.PageDto;
import com.zjhc.flyhong.common.web.Result;
import com.zjhc.web.controller.WebController;

/**
 * <h1>用户管理 控制器 </h1>
 * @创建人 ray
 * @创建时间 2017-08-12
 * @版本 1.0
 * @重要修改备注 
 * 
 */
@Controller
@RequestMapping("/system/user")
public class UserController extends WebController{
	@Autowired
	private OrganizationService organizationService;
	@Autowired
	private UserService userService;
	@Autowired
	private RoleService roleService;
	@Autowired
	private ButtonService buttonService;
	@Autowired
	private DictService dictService;
	@Autowired 
	private DictLoadUtil DictLoadUtil;
	/**
	 * 部门页面跳转
	 * @return
	 */
	@RequestMapping(value = "view")
	@RequiresPermissions("user:view")
	public ModelAndView view(String menuId, HttpServletRequest request, HttpServletResponse respose,
			ModelAndView modelAndView){
	  LoginUser user = getLoginUser();
	  //查询页面拥有的资源
	  Map<String,List<Button>> buttons= buttonService.queryUserButtonsWithMenu(user,menuId);
	  //获取字典信息
	  Map<String,List<Dict>> dicts = DictLoadUtil.loadDict(dictService,"userType","sex","useStatus");
	 
	  modelAndView.addObject("dicts", dicts);
	  modelAndView.addObject("buttons", buttons);
	  modelAndView.setViewName("system/user/view");
	  return modelAndView;
	}
	
	/**
	 * 新增页面
	 * @return
	 */
	@RequestMapping(value = "userAddView")
	@RequiresPermissions("user:add")
	public ModelAndView userAddView(String menuId,String orgName,User record, HttpServletRequest request, HttpServletResponse respose,
			ModelAndView modelAndView){
	  LoginUser user = getLoginUser();
	  //查询页面拥有的资源
	  Map<String,List<Button>> buttons= buttonService.queryUserButtonsWithMenu(user,menuId);
	  //获取字典信息
	  Map<String,List<Dict>> dicts = DictLoadUtil.loadDict(dictService,"userType","sex","useStatus");
	 
	  modelAndView.addObject("dicts", dicts);
	  modelAndView.addObject("buttons", buttons);
	  modelAndView.addObject("orgName", orgName);
	  modelAndView.addObject("record", record);
	  modelAndView.setViewName("system/user/add");
	  return modelAndView;
	}
	
	/**
	 * 组织新增
	 * @return
	 */
	@RequestMapping(value = "userAdd")
	@RequiresPermissions("user:add")
	@ResponseBody
	public ResponseEntity<Result<Object>> userAdd(User params, HttpServletRequest request, HttpServletResponse respose){
		LoginUser loginUser = getLoginUser();
		Result<Object> result = userService.addUser(params,loginUser);
		return doResult(result);
	}
	
	
	/**
	 * 组织修改页面
	 * @return
	 */
	@RequestMapping(value = "userUpdateView")
	@RequiresPermissions("user:edit")
	public ModelAndView userUpdateView(String menuId,String id, HttpServletRequest request, HttpServletResponse respose,
			ModelAndView modelAndView){
	  LoginUser user = getLoginUser();
	  //查询页面拥有的资源
	  Map<String,List<Button>> buttons= buttonService.queryUserButtonsWithMenu(user,menuId);
	  //查询菜单
	  User record = userService.queryUser(id,user);

	  Organization org = organizationService.queryOrg(record.getOrgCode(), user);
	  
	  //获取字典信息
	  Map<String,List<Dict>> dicts = DictLoadUtil.loadDict(dictService,"userType","sex","useStatus");
	 
	  modelAndView.addObject("dicts", dicts);
	  modelAndView.addObject("buttons", buttons);
	  modelAndView.addObject("orgName", org.getOrgName());
	  modelAndView.addObject("record", record);
	  modelAndView.setViewName("system/user/update");
	  return modelAndView;
	}
	/**
	 * 组织修改
	 * @return
	 */
	@RequestMapping(value = "userUpdate")
	@RequiresPermissions("user:edit")
	@ResponseBody
	public ResponseEntity<Result<Object>> userUpdate(User params, HttpServletRequest request, HttpServletResponse respose){
		LoginUser loginUser = getLoginUser();
		Result<Object> result = userService.updateUser(params,loginUser);
		return doResult(result);
	}
	
	/**
	 * 组织修改页面
	 * @return
	 */
	@RequestMapping(value = "userDetailView")
	@RequiresPermissions("user:detail")
	public ModelAndView userDetailView(String menuId,String id, HttpServletRequest request, HttpServletResponse respose,
			ModelAndView modelAndView){
	  LoginUser user = getLoginUser();
	  //查询页面拥有的资源
	  Map<String,List<Button>> buttons= buttonService.queryUserButtonsWithMenu(user,menuId);
	  //查询菜单
	  User record = userService.queryUser(id,user);
	  
	  Organization org = organizationService.queryOrg(record.getOrgCode(), user);
	  //获取字典信息
	  Map<String,List<Dict>> dicts = DictLoadUtil.loadDict(dictService,"userType","sex","useStatus");
	 
	  modelAndView.addObject("dicts", dicts);
	  modelAndView.addObject("buttons", buttons);
	  modelAndView.addObject("orgName", org.getOrgName());
	  modelAndView.addObject("record", record);
	  modelAndView.setViewName("system/user/detail");
	  return modelAndView;
	}
	
	/**
	 * 组织删除
	 * @return
	 */
	@RequestMapping(value = "userDelete")
	@RequiresPermissions("user:delete")
	@ResponseBody
	public ResponseEntity<Result<Object>> userDelete(String id, HttpServletRequest request, HttpServletResponse respose){
		LoginUser loginUser = getLoginUser();
		Result<Object> result = userService.deleteUser(id,loginUser);
		return doResult(result);
	}
	/**
	 * 租户页面跳转
	 * @return
	 */
	@RequestMapping(value = "query")
	@RequiresPermissions("user:query")
	@ResponseBody
	public ResponseEntity<Map<String,Object>> query(User params, HttpServletRequest request, HttpServletResponse respose){
		LoginUser loginUser = getLoginUser();
		PageDto<User> page = this.createJQPage(request);
		page = userService.queryUserList(params, page, loginUser);
		return doSuccess(DataGridUtil.dataConvert(page));
	}
	
	/**
	 * 设置管理员
	 * @return
	 */
	@RequestMapping(value = "userSetManager")
	@RequiresPermissions("user:setManager")
	@ResponseBody
	public ResponseEntity<Result<Object>> userSetManager(String id, HttpServletRequest request, HttpServletResponse respose){
		LoginUser loginUser = getLoginUser();
		Result<Object> result = userService.addManagerUser(id, loginUser);
		return doResult(result);
	}
	
	/**
	 * 取消管理员
	 * @return
	 */
	@RequestMapping(value = "userCancelManager")
	@RequiresPermissions("user:cancelManager")
	@ResponseBody
	public ResponseEntity<Result<Object>> userCancelManager(String id, HttpServletRequest request, HttpServletResponse respose){
		LoginUser loginUser = getLoginUser();
		Result<Object> result = userService.cancelManagerUser(id, loginUser);
		return doResult(result);
	}
	
	/**
	 * 取消管理员
	 * @return
	 */
	@RequestMapping(value = "userResetPassword")
	@RequiresPermissions("user:resetPassword")
	@ResponseBody
	public ResponseEntity<Result<Object>> userResetPassword(String id, HttpServletRequest request, HttpServletResponse respose){
		LoginUser loginUser = getLoginUser();
		Result<Object> result = userService.resetPassword(id, loginUser);
		return doResult(result);
	}
	
	/**
	 * 未拥有角色查询
	 * @return
	 */
	@RequestMapping(value = "notHaveRoleQuery")
	@RequiresPermissions("user:roleNotHaveQuery")
	@ResponseBody
	public ResponseEntity<Map<String,Object>> notHaveRoleQuery(Role params,String userId, HttpServletRequest request, HttpServletResponse respose){
		LoginUser loginUser = getLoginUser();
		PageDto<Role> page = this.createJQPage(request);
		page = roleService.queryNotHaveRoleList(params,userId, page, loginUser);
		return doSuccess(DataGridUtil.dataConvert(page));
	}
	
	
	/**
	 * 未拥有角色查询
	 * @return
	 */
	@RequestMapping(value = "haveRoleQuery")
	@RequiresPermissions("user:roleHaveQuery")
	@ResponseBody
	public ResponseEntity<Map<String,Object>> haveRoleQuery(Role params,String userId, HttpServletRequest request, HttpServletResponse respose){
		LoginUser loginUser = getLoginUser();
		PageDto<Role> page = this.createJQPage(request);
		page = roleService.queryHaveRoleList(params, userId,page, loginUser);
		return doSuccess(DataGridUtil.dataConvert(page));
	}
	
	/**
	 * 组织修改页面
	 * @return
	 */
	@RequestMapping(value = "userRoleView")
	@RequiresPermissions("user:role")
	public ModelAndView userRoleView(String menuId,String id, HttpServletRequest request, HttpServletResponse respose,
			ModelAndView modelAndView){
	  LoginUser user = getLoginUser();
	  //查询页面拥有的资源
	  Map<String,List<Button>> buttons= buttonService.queryUserButtonsWithMenu(user,menuId);
	  modelAndView.addObject("buttons", buttons);
	  modelAndView.addObject("userId", id);
	  modelAndView.setViewName("system/user/role");
	  return modelAndView;
	}
	
	/**
	 * 添加角色
	 * @return
	 */
	@RequestMapping(value = "addRole")
	@RequiresPermissions("user:roleAdd")
	@ResponseBody
	public ResponseEntity<Result<Object>> addRole(String roleId,String userId, HttpServletRequest request, HttpServletResponse respose){
		LoginUser loginUser = getLoginUser();
		Result<Object> result = roleService.addRoleRel(roleId,userId, LinkType.USER.getType(), loginUser);
		return doResult(result);
	}
	
	/**
	 * 删除角色
	 * @return
	 */
	@RequestMapping(value = "deleteRole")
	@RequiresPermissions("user:roleDelete")
	@ResponseBody
	public ResponseEntity<Result<Object>> deleteRole(String roleId,String userId, HttpServletRequest request, HttpServletResponse respose){
		LoginUser loginUser = getLoginUser();
		Result<Object> result = roleService.deleteRoleRel(roleId,userId,LinkType.USER.getType(), loginUser);
		return doResult(result);
	}
	
	
}
